/*NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

	Copyright (c) 2009 Jounayd Id Salah.
	All rights reserved.

	This file is part of Blacklib.

	Blacklib is free software: you can redistribute it and/or modify it under the 
	terms of the GNU Lesser General Public License as published by the Free Software 
	Foundation, either version 3 of the License, or (at your option) any later version.
	Blacklib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
	without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
	PURPOSE.  See the GNU Lesser General Public License for more details.
	You should have received a copy of the GNU Lesser General Public License along with 
	Blacklib.  If not, see <http://www.gnu.org/licenses/>.
*/
//NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN

#include "maths/sphere.h"

namespace black
{

	//__________________________________________________________________________
	const sphere sphere::UNIT = sphere( vec3::ZERO, 1.f );

	//__________________________________________________________________________
	void sphere::fastSet( const vec3 pPoints[], const size_t szNbPoints )
	{
		vec3 vMin( pPoints[ 0 ] );
		vec3 vMax( pPoints[ 0 ] );
		for ( size_t i=1; i < szNbPoints; i++ )
		{
			const vec3& p = pPoints[i];
			if ( p.x < vMin.x )
				vMin.x = p.x;
			else if ( p.x > vMax.x )
				vMax.x = p.x;

			if ( p.y < vMin.y )
				vMin.y = p.y;
			else if ( p.y > vMax.y )
				vMax.y = p.y;

			if ( p.z < vMin.z )
				vMin.z = p.z;
			else if ( p.z > vMax.z )
				vMax.z = p.z;
		}
		const vec3 vDiff = vMax - vMin;
		center = ( vMin + vMax ) * .5f;
		radius = vDiff.fastLength() * .5f;
	}
}
